package uit.com.project.iCampus.Util;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import uit.com.project.iCampus.application.ICampusApp;
import uit.com.project.iCampus.model.BuildingInfo;
import uit.com.project.iCampus.model.BuildingTypeEnum;
import uit.com.project.iCampus.services.ICampusAndroidServices;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Point;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;

public class Util {
	
	public static HashMap<String, String> MAP_STRING = new HashMap<String, String>();
	
	
	public static boolean isValidEmail(String email) {
		Pattern pattern;
		Matcher matcher;
		String EMAIL_PATTERN = "^[_A-Za-z0-9-]+(\\.[_A-Za-z0-9-]+)*@[A-Za-z0-9]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$";
		pattern = Pattern.compile(EMAIL_PATTERN);
		matcher = pattern.matcher(email);
		return matcher.matches();
	}
	
	@SuppressWarnings("rawtypes")
	public static String listToString(ArrayList<Class> list){
		String result = "";
		for (Class c : list) {
			result += c.getName() + "\t";
		}
		return result;
	}
	
	public static Date convertXMLStringToDate(String s){
		String yearMonthDate = s.substring(0, 10);
		String time = s.substring(11, 19);
		GregorianCalendar calendar = new GregorianCalendar();
		StringTokenizer tokenizer = new StringTokenizer(yearMonthDate, "-");
		int year = Integer.parseInt(tokenizer.nextToken());
		int month = Integer.parseInt(tokenizer.nextToken());
		int date = Integer.parseInt(tokenizer.nextToken());
		tokenizer = new StringTokenizer(time, ":");
		int hour = Integer.parseInt(tokenizer.nextToken());
		int minute = Integer.parseInt(tokenizer.nextToken());
		int second = Integer.parseInt(tokenizer.nextToken());
		calendar.set(year, month - 1, date, hour, minute, second);
		return calendar.getTime();
	}
	
	public static String dateTimeToStringIncludeHour(Date date) {
        return date.getHours() + ":" + date.getMinutes() + ", ngày " + date.getDate() + "/" + (date.getMonth() + 1) + "/" + (date.getYear() + 1900);
    }
	
	public static String dateTimeToString(Date date) {
        return date.getDate() + "/" + (date.getMonth() + 1) + "/" + (date.getYear() + 1900);
    }
	
	public static void showAlert(Context context, String title, String message){
		new AlertDialog.Builder(context).setTitle(title).setMessage(message)
		.setPositiveButton("OK", new DialogInterface.OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				//Do nothing
			}
		}).show();
	}
	
	public static List<BuildingInfo> getListBuildingWithBuildingTypeEnum(List<BuildingInfo> listMedicalCenter, BuildingTypeEnum buildingTypeEnum){
		List<BuildingInfo> result = new ArrayList<BuildingInfo>();
		
		// Check null
		if (listMedicalCenter != null) {
			for (BuildingInfo diaDiem : listMedicalCenter) {
				if (diaDiem.getBuidingTypeEnum().equals(buildingTypeEnum))
					result.add(diaDiem);
			}
		}
		return result;
	}
	
	public static void searchMedicalCenterInArea(MapView mapView){
		Point screenPts = new Point();
		mapView.getProjection().toPixels(ICampusApp.MY_LOCATION, screenPts);
		float radiusInPixel = mapView.getProjection().metersToEquatorPixels(ICampusApp.CIRCLE_RADIUS);
		Point topRight = new Point((int)(screenPts.x + radiusInPixel), (int)(screenPts.y - radiusInPixel));
		Point botLeft = new Point((int)(screenPts.x - radiusInPixel), (int)(screenPts.y + radiusInPixel));
		
		GeoPoint topRightPoint = mapView.getProjection().fromPixels(topRight.x, topRight.y);
		GeoPoint botLeftPoint = mapView.getProjection().fromPixels(botLeft.x, botLeft.y);
		ICampusApp.LIST_BUILDINGCENTER_IN_AREA = ICampusAndroidServices.searchListBuildingInfoInRadius(
				String.valueOf((botLeftPoint.getLatitudeE6() / 1E6)),
				String.valueOf((topRightPoint.getLatitudeE6() / 1E6)),
				String.valueOf((botLeftPoint.getLongitudeE6() / 1E6)),
				String.valueOf((topRightPoint.getLongitudeE6() / 1E6)));
		ICampusApp.LIST_BUILDING_FOOD_IN_AREA = Util.getListBuildingWithBuildingTypeEnum(ICampusApp.LIST_BUILDINGCENTER_IN_AREA, BuildingTypeEnum.FOODS);
		ICampusApp.LIST_BUILDING_TRAMS_IN_AREA = Util.getListBuildingWithBuildingTypeEnum(ICampusApp.LIST_BUILDINGCENTER_IN_AREA, BuildingTypeEnum.TRAMS);
		ICampusApp.LIST_BUILDING_University_IN_AREA = Util.getListBuildingWithBuildingTypeEnum(ICampusApp.LIST_BUILDINGCENTER_IN_AREA, BuildingTypeEnum.UNIVERSITY);
		ICampusApp.LIST_BUILDING_Softdrink_IN_AREA = Util.getListBuildingWithBuildingTypeEnum(ICampusApp.LIST_BUILDINGCENTER_IN_AREA, BuildingTypeEnum.SOFTDRINK);
		ICampusApp.LIST_BUILDING_HighBuilding_IN_AREA = Util.getListBuildingWithBuildingTypeEnum(ICampusApp.LIST_BUILDINGCENTER_IN_AREA, BuildingTypeEnum.HIGHBUILDING);
	}
	
	
	public static String checkNull(String value){
		return (value == null || value.equals("") ? ICampusApp.NULL_VALUE : value);
	}
	
	public static String formatHTML(String value){
		String result = checkNull(value);
        if (! result.contains("<li>"))
            result += "<br/><br/>";
        return result;
	}
	
}
